package org.example;

import org.apache.hadoop.io.*;


/**
 * Hadoop数据类型
 */
public class HadoopDataType {
    public static void main(String[] args) {
//        testText();
//        testArrayWritable();
        testMapWritable();
    }

    /**
     * Hadoop的Text类型数据
     */
    public static void testText() {
        Text text = new Text("Hello Hadoop");
        System.out.println(text.getLength());
        System.out.println(text.find("a"));
        System.out.println(text.toString());
    }

    /**
     * Hadoop的ArrayWritable
     */
    public static void testArrayWritable() {
        //声明的是整数类型的数组
        ArrayWritable arrayWritable = new ArrayWritable(IntWritable.class);
        //创建了三个整数类型
        IntWritable year = new IntWritable(2024);
        IntWritable month = new IntWritable(10);
        IntWritable day = new IntWritable(26);
        //把这三个数字添加到数组中
        arrayWritable.set(new IntWritable[]{year, month, day});
        System.out.println(arrayWritable.get()[0] instanceof IntWritable);
        System.out.println(
                String.format(
                        "year=%d,month=%d,date=%d",
                        ((IntWritable) (arrayWritable.get()[0])).get(),
                        ((IntWritable) arrayWritable.get()[1]).get(),
                        ((IntWritable) arrayWritable.get()[2]).get())
        );
    }

    /**
     * MapWritable
     */
    public static void testMapWritable(){
        MapWritable map = new MapWritable();
        Text k1 = new Text("name");
        Text v1 = new Text("tonny");
        Text k2 = new Text("password");
        map.put(k1, v1);
        map.put(k2, NullWritable.get());
        System.out.println(map.get(k1).toString());
        System.out.println(map.get(k2).toString());
    }
}
